﻿using AutoMapper;
using CozeDomain.Entities;
using CozeDomain.IRepositories;
using CozeDomain.IServices;
using CozeDto.SysDicDtos;
using CozeDto.SysDicTypeDtos;
using D.UtilCore.Dtos;
using D.UtilFreesqlOrm;

namespace CozeDomain.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class SysDicTypeService : BaseService<SysDicTypeEntity>, ISysDicTypeService
    {
        private readonly IMapper _mapper;
        private readonly ISysDicEntityRepository _sysDicRepository;
        private readonly ISysDicTypeEntityRepository _repository;
        /// <summary>
        /// 构造注入
        /// </summary>
        public SysDicTypeService(IMapper mapper, ISysDicTypeEntityRepository repository, ISysDicEntityRepository sysDicRepository)
        {
            // 用于base方法
            base._baseDal = repository;
            this._repository = repository;
            _mapper = mapper;
            _sysDicRepository = sysDicRepository;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddSysDicTypeAsync(ReqAddSysDicType reqModel)
        {
            // 检查名称重复
            var exists = await _repository.ExistsAsync(m => m.UniqueCode == reqModel.UniqueCode || m.Name == reqModel.Name);
            if (exists)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "该信息已存在" };
            }

            var insertEntity = _mapper.Map<ReqAddSysDicType, SysDicTypeEntity>(reqModel);
            insertEntity.DeleteFlag = false;
            insertEntity.CreateTime = DateTime.Now;
            insertEntity.Id = D.UtilFreesqlOrm.FreesqlOrm.NewId;

            var dbres = await _repository.AddAsync(insertEntity);

            return dbres.NormalResponse();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> DeleteSysDicTypeAsync(List<string> ids)
        {
            // 根据id获取数据
            var delDicTypes = await _repository.GetListAsync(m => ids.Contains(m.Id));
            if (delDicTypes.Count > 0)
            {
                var list = delDicTypes.Select(m => m.UniqueCode);
                var exists = await _sysDicRepository.ExistsAsync(m => list.Contains(m.DicTypeUniqueCode));
                if (exists)
                {
                    return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "不可删除，有数据正在使用" };
                }

                var dbres = await _repository.DeleteAsync(m => ids.Contains(m.Id));
                return dbres.NormalResponse();
            }
            return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "无数据可删除" };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> UpdateSysDicTypeAsync(ReqUpdateSysDicType reqModel)
        {
            var updateEntity = await _repository.GetModelAsync(m => m.Id == reqModel.Id);
            if (updateEntity == null)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "该信息不存在" };
            }

            var exists = await _repository.ExistsAsync(m => (m.UniqueCode == reqModel.UniqueCode || m.Name == reqModel.Name) && m.Id != reqModel.Id);
            if (exists)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "该信息已存在" };
            }

            updateEntity.Name = reqModel.Name;
            updateEntity.Status = reqModel.Status;
            updateEntity.UniqueCode = reqModel.UniqueCode;
            updateEntity.Description = reqModel.Description;
            updateEntity.UpdateTime = DateTime.Now;
            updateEntity.UpdateUser = reqModel.UpdateUser;

            var dbres = await _repository.UpdateAsync(updateEntity);

            return dbres.NormalResponse();
        }

        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<RepSysDicTypeInfo>> GetSysDicTypeAsync(string id)
        {
            var dbres = await _repository.GetModelAsync(m => m.Id == id);
            var res = _mapper.Map<SysDicTypeEntity, RepSysDicTypeInfo>(dbres);
            return res.GetResponse();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageQuery"></param>
        /// <param name="async"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageModel<SysDicTypeEntity>>> GetPagesAsync(ReqQuerySysDicTypePage query)
        {
            var dbres = await _repository.GetPagesAsync(query);
            var res = new PageModel<SysDicTypeEntity>()
            {
                PageNo = query.PageNo,
                PageSize = query.PageSize,
                Data = dbres.Item2,
                TotalCount = dbres.Item1,
                TotalPage = query.PageSize > 0 ? (int)Math.Ceiling(dbres.Item1 / (double)query.PageSize) : 0
            };
            return res.GetResponse();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<RepSelect>>> QueryDicTypeSelect()
        {
            var res = await _repository.QueryDicTypeSelect();
            return res.GetResponse();
        }

    }
}
